using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectThongTinCongTy()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THONGTINCONGTY
			};
			return View(CreateViewModel(data));
		}



		public ActionResult DeleteThongTinCongTy(int id, int page = 1)
		{
			var result = _repThongTinCongTy.Delete(id);
			var data = new DataTransferViewModel
			{
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_THONGTINCONGTY,
				CurrentPage = page
			};
			return View("SelectThongTinCongTy", CreateViewModel(data));
		}

		public ActionResult DetailOfThongTinCongTy(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THONGTINCONGTY,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateThongTinCongTy(FormCollection forms)
		{
			string id = forms.GetValues("ThongTinCongTy_ID")[0];
			string namphattrien = forms.GetValues("ThongTinCongTy_NamPhatTrien")[0];
			string tencongty = forms.GetValues("ThongTinCongTy_TenCongTy")[0];
			string diachi = forms.GetValues("ThongTinCongTy_DiaChi")[0];
			string dienthoai = forms.GetValues("ThongTinCongTy_DienThoai")[0];
			string emailcongty = forms.GetValues("ThongTinCongTy_EmailCongTy")[0];
			string matkhauemailcongty = forms.GetValues("ThongTinCongTy_MatKhauEmailCongTy")[0];
			string gioithieu = forms.GetValues("ThongTinCongTy_GioiThieu")[0];

			var thongtincongty = _repThongTinCongTy.SelectByID(int.Parse(id));
			
            thongtincongty.NamPhatTrien = namphattrien;
			thongtincongty.TenCongTy = tencongty;
			thongtincongty.DiaChi = diachi;
			thongtincongty.DienThoai = dienthoai;
			thongtincongty.EmailCongTy = emailcongty;
			thongtincongty.GioiThieu = gioithieu;

            if (thongtincongty.MatKhauEmailCongTy != matkhauemailcongty)
            {
                thongtincongty.MatKhauEmailCongTy = Encryption.Encrypt(matkhauemailcongty);
            }

			var result = _repThongTinCongTy.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_THONGTINCONGTY,
				IntID = int.Parse(id),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfThongTinCongTy", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertThongTinCongTy(FormCollection forms)
		{
			string namphattrien = forms.GetValues("ThongTinCongTy_NamPhatTrien")[0];
			string tencongty = forms.GetValues("ThongTinCongTy_TenCongTy")[0];
			string diachi = forms.GetValues("ThongTinCongTy_DiaChi")[0];
			string dienthoai = forms.GetValues("ThongTinCongTy_DienThoai")[0];
			string emailcongty = forms.GetValues("ThongTinCongTy_EmailCongTy")[0];
			string matkhauemailcongty = forms.GetValues("ThongTinCongTy_MatKhauEmailCongTy")[0];
			string gioithieu = forms.GetValues("ThongTinCongTy_GioiThieu")[0];

			var newItem = new ThongTinCongTy
			{
				NamPhatTrien = namphattrien,
				TenCongTy = tencongty,
				DiaChi = diachi,
				DienThoai = dienthoai,
				EmailCongTy = emailcongty,
				MatKhauEmailCongTy = Encryption.Encrypt(matkhauemailcongty),
				GioiThieu = gioithieu
			};

			var result = _repThongTinCongTy.Insert(newItem);

			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_THONGTINCONGTY,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectThongTinCongTy", CreateViewModel(data));
		}


	}
}